// sub_jigouguanli/jigoulist/jigoulist.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isYinjian: false,
    serachValue: "",
    list: [],
    threeBankList: [],
    isManage: false, //是否管理
    ischecked: false, //是否全选
    selectCheckbox: [], //选择列表
    pageNum: 1,
    pageSize: 50,
    organizationId: '',
    pageLevel: '',
    firstId: '' // 一级行id
  },
  // sec_admin 二级
  // cbrc 银监
  // fir_admin 一级
  // manager 客户经理
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    this.isFiradmin()
    const pageLevel = options.pageLevel || ''
    this.setData({
      organizationId: options.organizationId,
      pageLevel: pageLevel,
      firstId: options.firstId || ''
    })
    switch (pageLevel) {
      case '1':
        this.getListById()
        break;
      case '2':
        this.getListById()
        break;
      default:
        this.getList()
        break;
    }
  },
  getList () {
    let id = wx.getStorageSync("userInfo").agencyCode
    // 41d1f7cf6b874178a72d034caa454649"
    let params = {
      organizationId: id,
      name: this.data.serachValue,
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize
    }
    console.log(params);
    wx.showLoading({
      title: "加载中",
    });
    getApp().globalData.request(
      "/corpVisit/queryOrganizationPage", params,
      "POST"
    ).then(res => {
      wx.hideLoading();
      console.log(res,'接口调用成功');
      let filterList = ['渣打银行','创新工场','北京银行','金融租赁','国发融资担保','融风科技', '浦发硅谷银行苏州分行']
      for (let a in filterList) {
        for (let i in res.data.list) {
          if (res.data.list[i].name&&res.data.list[i].name.includes(filterList[a])) {
            res.data.list.splice(i, 1)
          }
        }
      }
      console.log(res,'接口调用成功2');

      this.setData({
        list: this.data.list.concat(res.data.list)
      })
    })
  },
  getThreeBankList () {
    getApp().globalData.request(
      "/corpVisit/getAllOrganizationTrdList", {
      name: this.data.serachValue
    },
      "POST"
    ).then(res => {
      console.log(res);
      this.setData({
        threeBankList: res.data,
      })
    })
  },
  getListById () {
    getApp().globalData.request(
      "/corpVisit/getOrganizationList", {
      name: this.data.serachValue,
      organizationId: this.data.organizationId
    },
      "POST"
    ).then(res => {
      console.log(res, 'getOrganizationList');
      this.setData({
        list: res.data,
      })
    })
  },
  // 判断是否是银监
  isFiradmin () {
    let arr = wx.getStorageSync("userInfo").corpRole;
    // let result = arr.find((item,index)=>{
    //   return item == "fir_admin"
    // })
    if (arr.indexOf("cbrc") !== -1) {
      this.setData({
        isYinjian: true
      })
    } else {
      this.setData({
        isYinjian: false
      })
    }
  },
  searchChange (e) {
    this.setData({
      serachValue: e.detail,
      pageNum: 1,
      list: []
    })
    this.getList()
  },
  searchThresbankChange (e) {
    getApp().globalData.request(
      "/corpVisit/getAllOrganizationTrdList", {
      name: e.detail
    },
      "POST"
    ).then(res => {
      console.log(res);
      this.setData({
        list: res.data,
      })
    })
  },
  toAddClick () {
    wx.navigateTo({
      url: '/sub_jigouguanli/jigouadd/jigouadd',
    })
  },
  toEditClick (e) {
    console.log(e.currentTarget.dataset.orizid);
    wx.navigateTo({
      url: `/sub_jigouguanli/jigouedit/jigouedit?orizid=${e.currentTarget.dataset.orizid}&firstId=${this.data.firstId}`,
    })
  },
  goDownLevel (e) {
    if (e.currentTarget.dataset.name == '一级行') {
      wx.navigateTo({
        url: `/sub_jigouguanli/jigoulist/jigoulist?organizationId=${e.currentTarget.dataset.id}&pageLevel=1`,
      });
      return
    } {
      if(this.data.organizationId) {
        wx.navigateTo({
          url: `/sub_jigouguanli/jigoulist/jigoulist?organizationId=${e.currentTarget.dataset.id}&firstId=${this.data.organizationId}&pageLevel=2`,
        });
      } else {
        wx.navigateTo({
          url: `/sub_jigouguanli/jigoulist/jigoulist?organizationId=${e.currentTarget.dataset.id}&pageLevel=2`,
        });
      }
    }
  },
  toFenzhihangClick () {
    console.log(this.data.selectCheckbox);
    if (this.data.selectCheckbox.length > 0) {
      wx.navigateTo({
        url: '/sub_jigouguanli/jigoufenzhihang/jigoufenzhihang?organizationIds=' + JSON.stringify(this.data.selectCheckbox),
      })
    }else{
      wx.showToast({
        title: '清先选择您要管理的机构！',
        icon:"none"
      })
    }
    
  },
  // 管理
  toManageClick () {
    this.setData({
      isManage: true
    })
  },
  noManageClick () {
    this.setData({
      isManage: false,
    })
  },
  // 多选
  selectChange (event) {
    console.log(event);
    if (event.detail.length == this.data.list.length) {
      this.setData({
        ischecked: true
      })
    } else {
      this.setData({
        ischecked: false
      })
    }
    this.setData({
      selectCheckbox: event.detail,
    });

  },

  ischeckedChange (e) {
    console.log(e);
    if (e.detail) {
      this.data.list.forEach((item, index) => {
        this.data.selectCheckbox.push(item.organizationId)
      })
      this.setData({
        selectCheckbox: this.data.selectCheckbox
      })
    } else {
      this.setData({
        selectCheckbox: []
      })
    }
    this.setData({
      ischecked: e.detail,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {
    if (this.data.refresh) {
      this.setData({
        list: [],
        pageNum: 1
      })
      this.getList()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh () {

  },
  // 滑动底部
  // onReachBottom: function () {
  //   this.scrollBottom();
  // },
  // 滑动底部加载更多
  scrollBottom () {
    if(!this.data.organizationId) {
      // this.onBottom();
      this.setData({
        pageNum: ++this.data.pageNum,
      });
      this.getList();
    }
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage () {

  }
})